﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using GRWL.BZSC.UI.CustomCtrl;
using GRWL.BZSC.UI.BaseDataSvc;

namespace GRWL.BZSC.UI
{
    partial class DeptFrm : BaseFrm
    {


        /// <summary>
        /// 选择器窗体返回项
        /// </summary>
        public SelectItem SelectorItem { get; set; }

        /// <summary>
        /// 编码规则
        /// </summary>
        //public CodeRuleFrm.CodeRule codeRule { get; set; }

        
        public DeptFrm()
        {
            InitializeComponent();

            this.BaseDataProxy = new BaseDataClient();
            this.BaseDataProxy.Open();
            this.codeRule = new CodeRuleFrm.CodeRule();//取默认编码规则
        }

        private void EditDeptFrm_Load(object sender, EventArgs e)
        {
            GRWL.BZSC.Common.EventCommon.DeptUdTVEvnt += new GRWL.BZSC.Common.EventCommon.UpdateTreeViewDlgt(updateTreeView);
            BindTreeView();
            this.codeRule.autoCode = false;
        }
        /// <summary>
        /// 接收处理UpdateTreeViewDlgt
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="arg"></param>
        private void updateTreeView(object sender,EventArgs arg)
        {
           TreeNode node = new TreeNode();
           GRWL.BZSC.UI.Common.OperateType opType = Common.OperateType.Add;
           CommonEventArg<TB_Department> argObj= (CommonEventArg<TB_Department>)arg;
           node.Name = argObj.t.id.ToString();
           node.Text = argObj.t.name;
           opType = argObj.OpType;

           if (uTreeView1.SelectedNode != null)
           {
               if (opType == Common.OperateType.Add)
                   this.uTreeView1.SelectedNode.Nodes.Add(node);
               else if (opType == Common.OperateType.Update)
                   this.uTreeView1.SelectedNode.Text = node.Text;
           }
        }

        private void BindTreeView()
        {
            TB_Department[] tb_Department = this.BaseDataProxy.FindDepartments(null,null,null);
           
            Common.BindDeptTree(uTreeView1,tb_Department,"部门列表");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            EditDeptFrm frm = new EditDeptFrm();
            frm.EditMode = true;
            frm.codeRule = this.codeRule;
            frm.ShowDialog(this);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.uTreeView1.SelectedNode.Name))
            {
                EditDeptFrm frm = new EditDeptFrm();
                frm.Id = int.Parse(uTreeView1.SelectedNode.Name);
                frm.EditMode = false;
                frm.ShowDialog(this);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.uTreeView1.SelectedNode.Name))
            {
                if (MessageBox.Show("确认删除该项吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    bool    ok = this.BaseDataProxy.DeleteDepartment(Convert.ToInt32(this.uTreeView1.SelectedNode.Name));
                    if (ok)
                        this.uTreeView1.Nodes.Remove(this.uTreeView1.SelectedNode);
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                CodeRuleFrm frm = new CodeRuleFrm("部门资料编码规则");
                frm.Rule = this.codeRule;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    this.codeRule = frm.Rule;
                }
            }

        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.uTreeView1.SelectedNode.Name))
            {
                SelectItem item = new SelectItem();
                if (this.uTreeView1.SelectedNode.Tag != null)
                    item.Key = this.uTreeView1.SelectedNode.Tag.ToString();
                item.Name = this.uTreeView1.SelectedNode.Text;
                item.Value = this.uTreeView1.SelectedNode.Name;
                this.SelectorItem = item;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);
            GRWL.BZSC.Common.EventCommon.DeptUdTVEvnt -= new GRWL.BZSC.Common.EventCommon.UpdateTreeViewDlgt(updateTreeView);
            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked)
                this.codeRule.autoCode = true;
            else
                this.codeRule.autoCode = false;
        }

    }
}
